use tokio::{net::TcpListener, sync::watch};

use bc_manager::BcManager;

#[tokio::main]
async fn main() {
    tracing_subscriber::fmt::init();

    let db_root_dir = std::path::PathBuf::from(bc_lib::DEFAULT_BC_DB_ROOT_DIR.to_string());

    let (mining_manager, _mining_cmd_receiver, _transaction_receiver) =
        bc_lib::mining::MiningManager::new();

    let (shutdown_tx, shutdown_rx) = watch::channel(false);
    let bc_manager = BcManager::new(db_root_dir, mining_manager, None, None, shutdown_rx).await;
    let app = bc_explorer::create_router(bc_manager.clone());

    let addr: std::net::SocketAddr = "127.0.0.1:3000".parse().unwrap();
    let listener = TcpListener::bind(addr).await.unwrap();
    println!(
        "🚀 Server of Blockchain Explorer running at http://{}",
        addr
    );
    axum::serve(listener, app).await.unwrap();
    shutdown_tx.send(true).unwrap();
    bc_manager.lock().await.shutdown().await;
}
